package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.service.MasterRPCService;
import id.net.iconpln.bb.client.service.MasterRPCServiceAsync;
import id.net.iconpln.bb.client.service.PengendalianRPCService;
import id.net.iconpln.bb.client.service.PengendalianRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbPengendalianBlackList extends LayoutContainer {
	private final PengendalianRPCServiceAsync service = GWT.create(PengendalianRPCService.class);
	private final MasterRPCServiceAsync masterService = GWT.create(MasterRPCService.class);
	private FormData formData;	
	private FormBinding formBindings;
	private Grid gridPltu,gridPeringatan;
	private ListStore storePltu,storePemasok,storePeringatan;
	private Button bSubmit;
	private ComboBox cboPemasok;
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());  
	    String mdl = Registry.get("isModule");
	    initStore();
	    
	    DateTimeFormat dt = DateTimeFormat.getFormat("dd/MM/YYYY");
	    formData = new FormData("-20");
	    
	    ContentPanel cp = new ContentPanel();    
	    cp.setHeaderVisible(false);
	    cp.setFrame(true);  
	    cp.setSize(1030, 600);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
 
	    getServiceInfo("master_pemasok","PEMASOK");
	    getServiceInfo("master_peringatan","PEMASOKBYSTATUS");
	    
	    

	    FormPanel pFormKontrak = createForm(); 
	    pFormKontrak.setScrollMode(Scroll.AUTO);
	    formBindings = new FormBinding(pFormKontrak, true);

	   
	    gridPeringatan = createGridPeringatan();
	    
	    ContentPanel pDelimeter = new ContentPanel();
	    pDelimeter.setWidth(10);
	    pDelimeter.setHeaderVisible(false);
	    pDelimeter.setLayout(new FitLayout());
	    
	    ToolBar tbKontrak = new ToolBar();
	    tbKontrak.add(new LabelToolItem("Data BlackList Pemasok"));
	    tbKontrak.add(new SeparatorToolItem());

	    
	    ContentPanel p = new ContentPanel();
	    p.setHeaderVisible(false);
	    p.setLayout(new RowLayout(Orientation.VERTICAL));	    	    
	    
//	    if (mdl.equals("PENGADAAN")){
//	    	p.add(pPltu, new RowData(.99, .4));
	    	p.add(pFormKontrak, new RowData(.99, 1));	    	
//	    }else{
//	    	p.add(pPltu, new RowData(.99, 1));
//	    }


	    ContentPanel pKirim = new ContentPanel();
	    pKirim.setHeaderVisible(false);
	    pKirim.setLayout(new RowLayout(Orientation.HORIZONTAL));
	    pKirim.add(gridPeringatan,new RowData(.99, 1));
	    pKirim.setTopComponent(tbKontrak);
	    
	    cp.add(p, new RowData(.4, 1));
	    cp.add(pKirim, new RowData(.6, 1));
	    
	    
	    add(cp, new FitData(5)); 
	  }  
	  
	        
	    private void initStore(){
//	      	storePltu = new ListStore();	
	      	storePeringatan = new ListStore();
	      	storePemasok= new ListStore();
	     }
	 
private void getServiceInfo(String ctrl,String kode){
	Map mapParam = new FastMap();
	if (ctrl.equals("master_pemasok")){
				masterService.getMaster(kode,mapParam,new AsyncCallback<List<Map>>() {
						public void onFailure(Throwable caught) {
							GWT.log(caught.getMessage(), null);
						}
						public void onSuccess(List<Map> result) {
					    	storePemasok.removeAll();
					    	storePemasok.add(ClientModule.setListBaseModelFromListMap(result));
//					    	storePemasok.unmask();
					}
				});
	}else if (ctrl.equals("master_peringatan")){ 	
		mapParam.put("status_pemasok","NULL");
		masterService.getMaster(kode,mapParam,new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}
			public void onSuccess(List<Map> result) {
				storePeringatan.removeAll();
				storePeringatan.add(ClientModule.setListBaseModelFromListMap(result));
				gridPeringatan.unmask();
		}
	});
//	}else if (ctrl.equals("master_pltu")) {
//		
//	service.getListPengiriman(kode,mapParam,new AsyncCallback<List<Map>>() {
//			public void onFailure(Throwable caught) {
//				GWT.log(caught.getMessage(), null);
//			}
//			public void onSuccess(List<Map> result) {
//		    	storePltu.removeAll();
//		    	storePltu.add(ClientModule.setListBaseModelFromListMap(result));
//		    	gridPltu.unmask();
//		}
//	});
//	}else if (ctrl.equals("master_kirim")) {
//		mapParam.put("id_pltu",Registry.get("id_pltu"));
//		service.getListPengiriman(kode,mapParam,new AsyncCallback<List<Map>>() {
//				public void onFailure(Throwable caught) {
//					GWT.log(caught.getMessage(), null);
//				}
//				public void onSuccess(List<Map> result) {
//			    	storePeringatan.removeAll();
//			    	storePeringatan.add(ClientModule.setListBaseModelFromListMap(result));
//			    	gridPeringatan.unmask();
//			}
//		});
	}
}

private Grid createGridPeringatan(){
	
	 List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

	    ColumnConfig column = new ColumnConfig();	    	     
	  
	    column = new ColumnConfig();  
	    column.setId("pemasok");  
	    column.setHeader("Pemasok");  
	    column.setWidth(150);  
	    configs.add(column);  	  
	    
	    
	    column = new ColumnConfig();  
	    column.setId("tanggal");  
	    column.setHeader("Tanggal");  
	    column.setWidth(100);  
	    configs.add(column);  	  

	    
	    column = new ColumnConfig();  
	    column.setId("keterangan");  
	    column.setHeader("Keterangan");  
	    column.setWidth(150);  
	    configs.add(column);  	  
	    

	    column = new ColumnConfig();  
	    column.setId("flag_blacklist");  
	    column.setHeader("BlackList");  
	    column.setWidth(100);  
	    configs.add(column);  	  

	    
	    storePeringatan.setMonitorChanges(true);
	    
	    final ColumnModel cm = new ColumnModel(configs);  
	
	    
	    gridPeringatan = new Grid(storePeringatan, cm);
//	    gridPeringatan.setView(view);  
	    gridPeringatan.setLoadMask(true);  
	    gridPeringatan.mask("load Data ...","x-mask-loading");
	    gridPeringatan.getView().setEmptyText("Data Empty");  
	    gridPeringatan.setBorders(false);  
//	    grid.setAutoExpandColumn("pemasok");  
	    gridPeringatan.setBorders(true);  
	  

			return gridPeringatan;
	}
private FormPanel createForm() {  
    FormPanel panel = new FormPanel();	    
    panel.setHeaderVisible(false);
    
    FieldSet fieldSet = new FieldSet();  
    fieldSet.setHeading("Entry BlackList");  
    fieldSet.setCollapsible(false);
    
  
    FormLayout layout = new FormLayout();  
    layout.setLabelWidth(110);  
    fieldSet.setLayout(layout);  
	
    
		cboPemasok = new ComboBox();    
		cboPemasok.setFieldLabel("Pemasok");
		cboPemasok.setEmptyText("Select Pemasok...");  
		cboPemasok.setDisplayField("pemasok");  
		cboPemasok.setWidth(150);  
		cboPemasok.setStore(storePemasok);  
		cboPemasok.setTypeAhead(true);  
		cboPemasok.setTriggerAction(TriggerAction.ALL);

		fieldSet.add(cboPemasok, formData);	
	    
		final DateField tglBlakuD = new DateField();
		tglBlakuD.setWidth(140);
		tglBlakuD.setFieldLabel("Tanggal");
		tglBlakuD.setPropertyEditor(new DateTimePropertyEditor("dd/MM/yyyy"));		
				
		fieldSet.add(tglBlakuD, formData);
//		
	    final Radio radio = new Radio();  
	    radio.setBoxLabel("Yes");
	    radio.setValueAttribute("0");
	  
	    Radio radio2 = new Radio();
	    radio2.setValueAttribute("1");
	    radio2.setBoxLabel("No");  
	  
	    final RadioGroup radioGroup = new RadioGroup();  
	    radioGroup.setFieldLabel("Black List");  
	    radioGroup.add(radio);  
	    radioGroup.add(radio2);    
	    
	    fieldSet.add(radioGroup, formData); 
	
	final TextArea keterangan = new TextArea();  
	keterangan.setName("keterangan");
	keterangan.setFieldLabel("Keterangan");    

	fieldSet.add(keterangan, formData);	  	  

     panel.add(fieldSet);

    ContentPanel p = new ContentPanel();
    p.setHeaderVisible(false);	    
    p.setButtonAlign(HorizontalAlignment.RIGHT);  
    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
      @Override  
      public void componentSelected(ButtonEvent ce) {  
//        grid.getStore().rejectChanges();  
      }  
    }));  
    bSubmit = new Button("Submit");
    p.addButton(bSubmit);
   
    bSubmit.addSelectionListener(new SelectionListener<ButtonEvent>() {
      @Override  
      public void componentSelected(ButtonEvent ce) {  

    	  MessageBox.confirm("Info", "Apakah Anda Yakin Ingin Menyimpan Data?",new Listener<MessageBoxEvent>() {

			@Override
			public void handleEvent(MessageBoxEvent be) {
				// TODO Auto-generated method stub
				Button btn = be.getButtonClicked();
				if (btn.getText().equals("Yes")){
				
		    	  	Map mapParam = new FastMap();
		    	  	mapParam.put("id_pemasok", cboPemasok.getValue().get("id_pemasok").toString());		
		    		mapParam.put("keterangan", keterangan.getValue());
		    		if (radio.getValue().equals(true)){
		    			mapParam.put("status_pemasok","0");
					}else{
						mapParam.put("status_pemasok","1");
					}
		    		mapParam.put("updateby", Registry.get("whoAmI").toString());
		    		mapParam.put("app_access", "WEB");
		    		mapParam.put("crud", "UPDATEBLACKLIST");
		    		
		    		Set set = mapParam.entrySet(); 
		    		Iterator i = set.iterator(); 
		    		// Display elements 
		    		while(i.hasNext()) { 
		    		Map.Entry me = (Map.Entry)i.next(); 
			    		GWT.log(me.getKey() + ": " + me.getValue()); 
		    		} 
		    			
		    		masterService.crudMaster("PEMASOK", mapParam,new AsyncCallback<List<Map>>() {
		    			public void onFailure(Throwable caught) {
		    				GWT.log(caught.getMessage(), null);
		    			}

		    			public void onSuccess(List<Map> result) {	    					    				
		    				gridPeringatan.mask("load Data ....","x-mask-loading");
		            		getServiceInfo("master_peringatan","PEMASOKBYSTATUS");
		    				
		    				MessageBox.alert("Informasi", " " + result, null);
		    			}
		    		});
				}		
	    		
			}
		});
      }  
    });  

    panel.add(p);
    
    return panel;  
  }  

}
